// pages/dateSelect/dateSelect.js

var Moment = require("../../utils/Moment.js");

var DATE_LIST = [];
var DATE_YEAR = new Date().getFullYear()
var DATE_MONTH = new Date().getMonth() + 1
var DATE_DAY = new Date().getDate()

var app = getApp()
var baseutil = require('../../utils/baseutil.js');
var config = require('../../utils/config.js');
var netutil = require('../../utils/netutil.js');
var util = require('../../utils/util.js');
let { WeToast } = require('../../src/wetoast.js');

Page({
  data: {
    maxMonth: 3, //最多渲染月数
    dateList: [],
    systemInfo: {},

    weekStr: ['日', '一', '二', '三', '四', '五', '六'],
    startDate: Moment(new Date()).format('YYYY-MM-DD'),
    endDate: Moment(new Date()).add(1, 'day').format('YYYY-MM-DD'),

    isStartDate: true    //是否是开始日期
  },
  onLoad: function (options) {
    // options.type  1表示国内机票   0表示火车票
    this.setData({
      type: options.type ,
      maxMonth: options.type == 1 ? 12 : 2
    });

    // 页面初始化 options为页面跳转所带来的参数
    this.createDateListData();
    var _this = this;
    // 页面初始化 options为页面跳转所带来的参数

    var startDate = options.startDate ? options.startDate : Moment(new Date()).format('YYYY-MM-DD');
    // var endDate = options.endDate ? options.endDate : Moment(new Date()).add(1, 'day').format('YYYY-MM-DD');
    var endDate = Moment(new Date()).add(1, 'day').format('YYYY-MM-DD');


 

    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          systemInfo: res,
          startDate: startDate,
          endDate: endDate,
          isStartDate: options.isStartDate == 1,
         
        });
      }
    })
    baseutil.log(this.data.isStartDate)
    baseutil.log(typeof (this.data.isStartDate))

    new app.WeToast()
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  createDateListData: function () {
    var dateList = [];
    var now = new Date();
    /*
      设置日期为 年-月-01,否则可能会出现跨月的问题
      比如：2017-01-31为now ,月份直接+1（now.setMonth(now.getMonth()+1)），则会直接跳到跳到2017-03-03月份.
        原因是由于2月份没有31号，顺推下去变成了了03-03
    */
    now = new Date(now.getFullYear(), now.getMonth(), 1);
    for (var i = 0; i < this.data.maxMonth; i++) {
      var momentDate = Moment(now).add(this.data.maxMonth - (this.data.maxMonth - i), 'month').date;
      var year = momentDate.getFullYear();
      var month = momentDate.getMonth() + 1;

      var days = [];
      var totalDay = this.getTotalDayByMonth(year, month);
      var week = this.getWeek(year, month, 1);
      //-week是为了使当月第一天的日期可以正确的显示到对应的周几位置上，比如星期三(week = 2)，
      //则当月的1号是从列的第三个位置开始渲染的，前面会占用-2，-1，0的位置,从1开正常渲染
      for (var j = -week + 1; j <= totalDay; j++) {
        var tempWeek = -1;
        if (j > 0)
          tempWeek = this.getWeek(year, month, j);
        var clazz = '';
        if (tempWeek == 0 || tempWeek == 6)
          clazz = 'week'
        if (j < DATE_DAY && year == DATE_YEAR && month == DATE_MONTH)
          //当天之前的日期不可用
          clazz = 'unavailable ' + clazz;
        else if (this.data.maxMonth == 2 && j > DATE_DAY-1 && year == DATE_YEAR && month == DATE_MONTH + 1){
          //当天之前的日期不可用
          clazz = 'unavailable ' + clazz;
          }
        else
          clazz = 'nostate ' + clazz
        days.push({ day: j, class: clazz })
      }
      var dateItem = {
        id: year + '-' + month,
        year: year,
        month: month,
        days: days
      }

      dateList.push(dateItem);
    }
    this.setData({
      dateList: dateList
    });
    DATE_LIST = dateList;
  },
  /*
	 * 获取月的总天数
	 */
  getTotalDayByMonth: function (year, month) {
    month = parseInt(month, 10);
    var d = new Date(year, month, 0);
    return d.getDate();
  },
	/*
	 * 获取月的第一天是星期几
	 */
  getWeek: function (year, month, day) {
    var d = new Date(year, month - 1, day);
    return d.getDay();
  },
  /**
   * 点击日期事件
   */
  onPressDate: function (e) {
    var { year, month, day } = e.target.dataset;

    //当前选择的日期为同一个月并小于今天，或者点击了空白处（即day<0），不执行
    if ((day < DATE_DAY && month == DATE_MONTH) || day <= 0) return;

    var tempMonth = month;
    var tempDay = day;

    if (month < 10) tempMonth = '0' + month
    if (day < 10) tempDay = '0' + day

    var date = year + '-' + tempMonth + '-' + tempDay;

    if (!(this.data.isStartDate) && Moment(date).before(this.data.startDate)
      // || this.data.startDate === date  
    ) {//结束日期是否小于等于返回日期
      console.log("结束日期小于等于返回日期!");
      return;
    }

    var now = new Date;
    if (this.data.maxMonth == 2 && now.getMonth() + 1!= tempMonth && tempDay >= now.getDate()) {
      //当天之前的日期不可用
      baseutil.log(now.getMonth()+"---"+tempDay + "---" + now.getDate())
      console.log("当前日期不可选!");
      return;
    }

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    if (this.data.isStartDate) {
      // prevPage.setData({
      //   goDate: date,
      //   goWeek: util.getTheDateWeek(date)
      // })
      if(this.data.type==1){//国内机票
        prevPage.updateTicketStartDate(date, util.getTheDateWeek(date))
      }else{//火车票
        prevPage.updateTrainStartDate(date, util.getTheDateWeek(date))
      }
    } else {
      
      if (this.data.type == 1) {//国内机票
        prevPage.updateTicketBackDate(date, util.getTheDateWeek(date))
      } else {//火车票
        prevPage.updateTrainBackDate(date, util.getTheDateWeek(date))
      }
    }

    baseutil.returnLastPage();
    console.log(date + "---" + util.getTheDateWeek(date));
    this.renderPressStyle(year, month, day);
  },
  renderPressStyle: function (year, month, day) {
    var dateList = this.data.dateList;
    //渲染点击样式
    for (var i = 0; i < dateList.length; i++) {
      var dateItem = dateList[i];
      var id = dateItem.id;
      if (id === year + '-' + month) {
        var days = dateItem.days;
        for (var j = 0; j < days.length; j++) {
          var tempDay = days[j].day;
          if (tempDay == day) {
            days[j].class = days[j].class + ' active';
            break;
          }
        }
        break;
      }
    }
    this.setData({
      dateList: dateList
    });
  }
})